﻿#region License

// Dimebrain TweetSharp
// (www.dimebrain.com)
// 
// The MIT License
// 
// Copyright (c) 2009 Dimebrain
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// 

#endregion

using System;
using System.Collections.Generic;
using System.Linq;

namespace Dimebrain.TweetSharp.Model
{
    /// <summary>
    /// Represents a geospatial location, for APIs that support it.
    /// </summary>
#if !SILVERLIGHT
    [Serializable]
#endif
    public struct GeoLocation : IEquatable<GeoLocation>
    {
        public double Latitude { get; internal set; }
        public double Longitude { get; internal set; }

        public GeoLocation(double latitude, double longitude)
            : this()
        {
            Latitude = latitude;
            Longitude = longitude;
        }

        public bool Equals(GeoLocation other)
        {
            return other.Latitude == Latitude 
                && other.Longitude == Longitude;
        }

        public override bool Equals(object instance)
        {
            if (ReferenceEquals(null, instance))
            {
                return false;
            }

            return instance.GetType() == typeof (GeoLocation) && Equals((GeoLocation) instance);

        }

        public override int GetHashCode()
        {
            unchecked
            {
                return (Latitude.GetHashCode() * 397) ^ Longitude.GetHashCode();
            }
        }

        public static bool operator ==(GeoLocation left, GeoLocation right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(GeoLocation left, GeoLocation right)
        {
            return !left.Equals(right);
        }

        public static explicit operator double[](GeoLocation location)
        {
            return new[]{location.Latitude, location.Longitude};
        }

        public static implicit operator GeoLocation(List<double> values)
        {
            return FromEnumerable(values);
        }

        public static implicit operator GeoLocation(double[] values)
        {
            return FromEnumerable(values);
        }

        private static GeoLocation FromEnumerable(IEnumerable<double> values)
        {
            if (values == null)
            {
                throw new ArgumentNullException("values");
            }

            var latitude = values.First();
            var longitude = values.Skip(1).Take(1).Single();

            return new GeoLocation(latitude, longitude);
        }
    }
}